package com.runa.monitor.platinterface.extend.third.comm.service;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.constants.ResultConstants;
import com.runa.monitor.comm.dto.newComm.*;
import com.runa.monitor.comm.entity.*;
import com.runa.monitor.platinterface.mqtt.MqttUtil;
import com.runa.monitor.platinterface.extend.third.pojo.Result;
import com.runa.monitor.platinterface.extend.third.pojo.TemperaturePressureCurrentData;
import com.runa.monitor.platinterface.extend.third.vo.DockingDataResponseVo;
import com.runa.monitor.platinterface.platService.RepositoryService;
import com.runa.persistence.util.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class CommThirdService {

    @Resource
    private RepositoryService repositoryService;

    public Result<List<DockingDataResponseVo>> batchSaveMeterData(String factory, String netDevice, List<HotMeterVO> hotMeterVOList) {
        List<DockingDataResponseVo> responseVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(hotMeterVOList)) {
            for (HotMeterVO hotMeterVO : hotMeterVOList) {
                CommMeter meter = repositoryService.getCommMeter(hotMeterVO.getMeterSerial(), factory, netDevice);
                if (meter == null) {
                    log.error("batchSaveHouseMeterData无数据库数据:" + JSON.toJSONString(hotMeterVO));
                    DockingDataResponseVo dockingDataResponseVo = new DockingDataResponseVo();
                    dockingDataResponseVo.setMeterSerialNo(hotMeterVO.getMeterSerial());
//                    dockingDataResponseVo.setFieldCode(objectNotExist);
                    dockingDataResponseVo.setReason("节点设备档案不存在");
                    responseVoList.add(dockingDataResponseVo);
                    continue;
                }
                AsynResult asynResult = new AsynResult();
                asynResult.setCode(ResultConstants.CODE_200);
                asynResult.setMsg(MqttUtil.successString);
                asynResult.setIdentity(meter.getId());
                asynResult.setContent(hotMeterVO);
                if (!hotMeterVO.getSuccess()) {
                    asynResult.unreadSetting();
                    asynResult.setContent(null);
                }
                LogUtil.getLogAndInfo(hotMeterVO.getMeterSerial(), "NB", "batchSaveMeterData: {}", JSON.toJSONString(hotMeterVO));
                MqttUtil.send(HotMeterVO.TOPIC, asynResult);
            }
        }
        return new Result<List<DockingDataResponseVo>>().handleSuccessResult(responseVoList);
    }

    public Result<List<DockingDataResponseVo>> batchSaveValveData(String factory, String netDevice, List<ValveVO> valveVOList) {
        List<DockingDataResponseVo> responseVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(valveVOList)) {
            for (ValveVO valveVO : valveVOList) {
                CommValve valve = repositoryService.getCommValve(valveVO.getValveSerial(), factory, netDevice);
                if (valve == null) {
                    log.error("batchSaveValveData无数据库数据:" + JSON.toJSONString(valveVO));
                    DockingDataResponseVo dockingDataResponseVo = new DockingDataResponseVo();
                    dockingDataResponseVo.setMeterSerialNo(valveVO.getValveSerial());
//                    dockingDataResponseVo.setFieldCode(objectNotExist);
                    dockingDataResponseVo.setReason("节点设备档案不存在");
                    responseVoList.add(dockingDataResponseVo);
                    continue;
                }
                AsynResult asynResult = new AsynResult();
                asynResult.setCode(ResultConstants.CODE_200);
                asynResult.setMsg(MqttUtil.successString);
                asynResult.setIdentity(valve.getId());
                asynResult.setContent(valveVO);
                if (!valveVO.getSuccess()) {
                    asynResult.unreadSetting();
                    asynResult.setContent(null);
                }
                LogUtil.getLogAndInfo(valveVO.getValveSerial(), "NB", "batchSaveValveData: {}", JSON.toJSONString(valveVO));
                MqttUtil.send(ValveVO.TOPIC, asynResult);
            }
        }
        return new Result<List<DockingDataResponseVo>>().handleSuccessResult(responseVoList);
    }

    public Result<List<DockingDataResponseVo>> batchSavePanelData(String factory, String netDevice, List<PanelVO> panelVOList) {
        List<DockingDataResponseVo> responseVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(panelVOList)) {
            for (PanelVO panelVO : panelVOList) {
                CommPanel panel = repositoryService.getCommPanel(panelVO.getSerial(), factory, netDevice);
                if (panel == null) {
                    log.error("batchSavePanelData无数据库数据:" + JSON.toJSONString(panelVO));
                    DockingDataResponseVo dockingDataResponseVo = new DockingDataResponseVo();
                    dockingDataResponseVo.setMeterSerialNo(panelVO.getSerial());
//                    dockingDataResponseVo.setFieldCode(objectNotExist);
                    dockingDataResponseVo.setReason("节点设备档案不存在");
                    responseVoList.add(dockingDataResponseVo);
                    continue;
                }
                AsynResult asynResult = new AsynResult();
                asynResult.setCode(ResultConstants.CODE_200);
                asynResult.setMsg(MqttUtil.successString);
                asynResult.setIdentity(panel.getId());
                asynResult.setContent(Collections.singleton(panelVO));
                if (!panelVO.getSuccess()) {
                    asynResult.unreadSetting();
                    asynResult.setContent(null);
                }
                LogUtil.getLogAndInfo(panelVO.getSerial(), "NB", "batchSavePanelData: {}", JSON.toJSONString(panelVO));
                MqttUtil.send(PanelVO.TOPIC, asynResult);
            }
        }
        return new Result<List<DockingDataResponseVo>>().handleSuccessResult(responseVoList);
    }

    public Result<List<DockingDataResponseVo>> batchSaveTemperaturePressureData(String factory, String netDevice, List<TemperaturePressureCurrentData> temperaturePressureCurrentDataList) {
        List<DockingDataResponseVo> responseVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(temperaturePressureCurrentDataList)) {
            for (TemperaturePressureCurrentData temperaturePressureCurrentData : temperaturePressureCurrentDataList) {
                CommTemperaturepressure commTemperaturepressure = repositoryService.getCommTemperaturepressure(temperaturePressureCurrentData.getSerial(), factory, netDevice);
                if (commTemperaturepressure == null) {
                    log.error("batchSaveTemperaturePressureData无数据库数据:" + JSON.toJSONString(temperaturePressureCurrentData));
                    DockingDataResponseVo dockingDataResponseVo = new DockingDataResponseVo();
                    dockingDataResponseVo.setMeterSerialNo(temperaturePressureCurrentData.getSerial());
//                    dockingDataResponseVo.setFieldCode(objectNotExist);
                    dockingDataResponseVo.setReason("节点设备档案不存在");
                    responseVoList.add(dockingDataResponseVo);
                    continue;
                }
                AsynResult asynResult = new AsynResult();
                asynResult.setCode(ResultConstants.CODE_200);
                asynResult.setMsg(MqttUtil.successString);
                asynResult.setIdentity(commTemperaturepressure.getId());
                asynResult.setContent(temperaturePressureCurrentData);
                if (!temperaturePressureCurrentData.getSuccess()) {
                    asynResult.unreadSetting();
                    asynResult.setContent(null);
                }
                LogUtil.getLogAndInfo(temperaturePressureCurrentData.getSerial(), "NB", "batchSaveTemperaturePressureData: {}", JSON.toJSONString(temperaturePressureCurrentData));
                MqttUtil.send(TemperaturePressureVo.TOPIC, asynResult);
            }
        }
        return new Result<List<DockingDataResponseVo>>().handleSuccessResult(responseVoList);
    }
}
